﻿using Abp.AutoMapper;
using Abp.Dependency;
using Abp.Modules;
using System.Reflection;
using Abp.Configuration.Startup;

using Abp.Threading.BackgroundWorkers;


using BXJG.Utils;
using Microsoft.Extensions.DependencyInjection;
using Hangfire;
using Abp.Collections.Extensions;
using Abp.Runtime.Session;

using Abp.Localization.Dictionaries.Xml;
using Abp.Localization.Dictionaries;
using ZLJ.Application.Authorization.Permissions;
using ZLJ.Application.Common;
using ZLJ.Application.Share;

namespace ZLJ.Application
{
    [DependsOn(typeof(CommonApplicationModule))]
    public class ZLJApplicationModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Navigation.Providers.Add<AdminNavigationProvider>();
            Configuration.Authorization.Providers.Add<AdminAuthorizationProvider>();
            //注册automapper映射
            Configuration.Modules.AbpAutoMapper().Configurators
                .Add(cfg => cfg.AddMaps(Assembly.GetExecutingAssembly()));

            Configuration.Localization.Sources.Add(
              new DictionaryBasedLocalizationSource(AdminConsts.Admin,
                  new XmlEmbeddedFileLocalizationDictionaryProvider(
                     Assembly.GetExecutingAssembly(),
                      "ZLJ.Application.Localization.SourceFiles"
                  )
              )
          );
            //不要替换，AdminSession单独注册算了
            //Configuration.ReplaceService<IAbpSession, AdminSession>(DependencyLifeStyle.Transient);
        }

        public override void Initialize()
        {
            //经过测试，这样abp还是无法生成动态webapi，手动提供实现类吧
            //IocManager.Register(typeof(IBXJGShopItemAppService), typeof(BXJGShopItemAppService<Tenant, User, Role, TenantManager, UserManager, GeneralTreeEntity>), DependencyLifeStyle.Transient);
            //IocManager.Register(typeof(IBXJGShopFrontItemAppService), typeof(BXJGShopFrontItemAppService<GeneralTreeEntity>), DependencyLifeStyle.Transient);
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            //IocManager.RegService(s => {
            //    s.AddHostedService<CustomerReportWorkerRegister>();
            //});

            //Configuration.Modules.AbpAutoMapper().Configurators.Add(cfg => cfg.AddProfile<BXJGShopMapProfile<User>>());

            //IocManager.RegisterBXJGWorkOrderDefaultAdapter<User>();//User为你项目的用户类型

            //集成工单模块需要的员工信息提供器
            //Configuration.ReplaceService<BXJG.WorkOrder.Employee.IEmployeeAppService, WorkOrderEmployeeService<User>>(DependencyLifeStyle.Transient);

            //IocManager.Register<BXJG.WorkOrder.Employee.IEmployeeAppService, WorkOrderEmployeeService<User>>(
            //    DependencyLifeStyle.Transient);
        }


        //string CustomerReportWorkerRegisterId = typeof(CustomerReportWorkerRegister).FullName;
        public override void PostInitialize()
        {
            var workManager = IocManager.Resolve<IBackgroundWorkerManager>();

          // var txq = IocManager.Resolve<IClusterClient>().GetGrain<ITongxinqi>("aaa");
          //  txq.ShezhiPeizhi(new Dictionary<string, string> { });
            //workManager.Add(IocManager.Resolve<clzxzt>());
            //workManager.Add(IocManager.Resolve<sccjjl>());
            //workManager.Add(IocManager.Resolve<EventBackgroundWork>());
            //workManager.Add(IocManager.Resolve<EquipmentReportDataConvert>());
            //workManager.Add(IocManager.Resolve<CustomerReportWorker>());

            //workManager.Add(IocManager.Resolve<SyncStaffToEquipmentInstanceWorkder>());
            //BackgroundJob.Schedule<CustomerReportWorkerRegister>( c => c.StartAsync(), TimeSpan.FromMilliseconds(1));
            //CustomerReportWorkerRegisterId = BackgroundJob.Enqueue<CustomerReportWorkerRegister>(c => c.StartAsync());

            //RecurringJob.AddOrUpdate<CustomerReportWorkerRegister>(CustomerReportWorkerRegisterId, c => c.StartAsync(), Cron.Hourly(Random.Shared.Next(1,59) ));//由于系统启动时可能有很多任务注册，避免大家挤一堆，搞个随机数
            //RecurringJob.Trigger(CustomerReportWorkerRegisterId);
        }

        public override void Shutdown()
        {
            //RecurringJob.RemoveIfExists(CustomerReportWorkerRegisterId);
            //BackgroundJob.Delete(CustomerReportWorkerRegisterId);
        }
    }
}